package com.zb.web.controller;

import com.zb.model.TGoodsInShopCart;
import com.zb.service.impl.ShopCartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web")
public class ShopCartController {

	@Autowired
	private ShopCartServiceImpl scs;
	
	
	
	@RequestMapping("/SelectShopCart")
	public String selectShopCart(HttpSession session,HttpServletRequest request){
		
		List<TGoodsInShopCart> shopcartList= scs.selectAllGoodsInSc((String)session.getAttribute("userid"));
		session.setAttribute("shopcartList", shopcartList);
		//request.getRequestDispatcher("jsp/shopcart.jsp");
		return "shopcart";
	}
	
	/**
	 * ��ȡ�����ﳵ����
	 * @param goodsId
	 * @param number
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/ShopCart")
	public String addGoodsIntoShopCart(String goodsId,int number,HttpSession session,HttpServletRequest request){
		number=Integer.parseInt(request.getParameter("number").toString());
		goodsId=request.getParameter("goodid");
		System.out.println("--------------"+number+goodsId);
//		session.setAttribute("sum", sum);
//		session.setAttribute("selectCount", selectCount);
		//System.out.println("@@@@@@@@@@@");
		List<TGoodsInShopCart> shopcartList= scs.addGoodsIntoShopCartService(goodsId,(String)session.getAttribute("userid"),number);
		session.setAttribute("shopcartList", shopcartList);
		//request.getRequestDispatcher("jsp/shopcart.jsp");
		return "shopcart";
	}
	/**
	 * ת���ﳵ������ʹ�ã�
	 * @param session
	 * @param map
	 * @return
	 */
	@RequestMapping("/GotoShopCart")
	public String GotoShopCart(HttpSession session,Map map){
		float sum=0;
		int selectCount=0;
		session.setAttribute("sum", sum);
		session.setAttribute("selectCount", selectCount);
		map.put("goodsList", scs.selectAllGoodsInSc((String)session.getAttribute("userid")));
		return "shopcart";
	}
	
	/**
	 * @param session
	 * @param map
	 * @return
	 */
	@RequestMapping("/AfterPayToShopCart")
	public String AfterPayToShopCart(HttpSession session,Map map){
		String userId=(String)session.getAttribute("userid");
		List<TGoodsInShopCart> goodsInScList=(List<TGoodsInShopCart>) session.getAttribute("goodsInScList");
		map.replace("goodsList", scs.afterPayToShopCart(userId,goodsInScList));
		return "shopcart";
	}
	
	@RequestMapping("/UpdateCount")
	@ResponseBody
	public TGoodsInShopCart UpdateCount(String goodsId,String method,HttpSession session){
		return scs.updateCount(goodsId, (String)session.getAttribute("userid"), method);
	}
	
	/**
	 * @param goodsId
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/DelGoodsInSc")
	@ResponseBody
	public String delGoodsInSc(String goodsId,HttpSession session,HttpServletRequest request){
		System.out.println("从购物车中删除");
		goodsId=request.getParameter("goodId");
		System.out.println((String)session.getAttribute("userid")+goodsId);
		
		List<TGoodsInShopCart> shopcartList = scs.delGoodsIntoShopCartService(goodsId,session.getAttribute("userid").toString());
		session.setAttribute("shopcartList", shopcartList);
		return "shopcart";
	}
	
	/**
	 * 从购物车中删除，new用法
	 * @param goodsId
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/DelGoodsInScNew")
	@ResponseBody
	public String delGoodsInScNew(String goodsId,HttpSession session,HttpServletRequest request){
		System.out.println("从购物车中删除");
		goodsId=request.getParameter("goodId");
		String orderId=request.getParameter("orderId");
		//System.out.println((String)session.getAttribute("userid")+goodsId);
		
		List<TGoodsInShopCart> shopcartList = scs.delGoodsIntoShopCartService(goodsId,session.getAttribute("userid").toString());
		session.setAttribute("shopcartList", shopcartList);
		return "shopcart";
	}
	
	/**
	 * ��ѯ�ܼۣ�����ʹ�ã�
	 * @param session
	 * @return
	 */
	@RequestMapping("/AllSelect")
	@ResponseBody
	public float AllSelect(HttpSession session){
		float sum=scs.allSelectSum((String)session.getAttribute("userid"),session);
		session.setAttribute("sum", sum);
		return sum;
	}
	
	/**
	 * ȡ��ȫѡ�۸񣨲���ʹ�ã�
	 * @param session
	 * @return
	 */
	@RequestMapping("/AllNotSelect")
	@ResponseBody
	public float AllNotSelect(HttpSession session){
		session.setAttribute("goodsInScList", null);
		session.setAttribute("sum", 0);
		return 0;
	}
	
	/**
	 * ĳһ����Ʒ�ύ����
	 * @param sum
	 * @param goodsId
	 * @param method
	 * @param session
	 * @return
	 */
	@RequestMapping("/SingleSelect")
	@ResponseBody
	public float SingleSelect(String sum,String goodsId,String method,HttpSession session){
		float sum1=scs.SingleSelectChangeSum(goodsId, (String)session.getAttribute("userid"),Float.parseFloat(sum),method,session);
		session.setAttribute("sum", sum1);
		return sum1;
	}
	
	@RequestMapping("/ShopCartPay")
	public String ShopCartPay(String goodsId,HttpSession session,Map map){

		return "shopcart_pay";
	}
	
}
